package com.sailfish.springbootdemo.service.db2.impl;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db2.PcbApproveConfigDao;
import com.sailfish.springbootdemo.dao.db2.PcbCapacityDataApproveDao;
import com.sailfish.springbootdemo.dao.db2.PcbParentFactoryInfoDao;
import com.sailfish.springbootdemo.dao.db2.PcbSonFactoryInfoDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db2.PcbApproveConfig;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityDataApprove;
import com.sailfish.springbootdemo.pojo.db2.PcbParentFactoryInfo;
import com.sailfish.springbootdemo.pojo.db2.PcbSonFactoryInfo;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db2.PcbSonFactoryInfoService;
import com.sailfish.springbootdemo.service.db2.S3UploadFileService;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PcbSonFactoryInfoServiceImpl implements PcbSonFactoryInfoService {

    @Autowired
    PcbSonFactoryInfoDao pcbSonFactoryInfoDao;
    @Autowired
    PcbParentFactoryInfoDao parentFactoryInfoDao;
    @Autowired
    PcbApproveConfigDao pcbApproveConfigDao;
    @Autowired
    PcbCapacityDataApproveDao pcbCapacityDataApproveDao;
    @Autowired
    OperateService operateService;
    @Autowired
    S3UploadFileService s3UploadFileService;

    @Override
    public List<PcbSonFactoryInfo> getAll(Integer parentId, String companyName, String companyNameEn, String companyWebsite,
                                          String sonFactoryName, String sonFactoryNameEn, String sonFactoryIntroduction,
                                          String sonFactoryPhone, String sonFactoryContacts, String sonFactoryEmail,
                                          String sonFactoryCategory, Integer status) {
        PcbSonFactoryInfo pcbSonFactoryInfo = new PcbSonFactoryInfo();
        pcbSonFactoryInfo.setParentId(parentId);
        pcbSonFactoryInfo.setCompanyName(companyName);
        pcbSonFactoryInfo.setCompanyNameEn(companyNameEn);
        pcbSonFactoryInfo.setCompanyWebsite(companyWebsite);
        pcbSonFactoryInfo.setSonFactoryName(sonFactoryName);
        pcbSonFactoryInfo.setSonFactoryNameEn(sonFactoryNameEn);
        pcbSonFactoryInfo.setSonFactoryIntroduction(sonFactoryIntroduction);
        pcbSonFactoryInfo.setSonFactoryPhone(sonFactoryPhone);
        pcbSonFactoryInfo.setSonFactoryContacts(sonFactoryContacts);
        pcbSonFactoryInfo.setSonFactoryCategory(sonFactoryCategory);
        pcbSonFactoryInfo.setSonFactoryEmail(sonFactoryEmail);
        pcbSonFactoryInfo.setStatus(status);
        List<PcbSonFactoryInfo> factoryInfos = pcbSonFactoryInfoDao.getAllByLimit(pcbSonFactoryInfo);
        for (PcbSonFactoryInfo item : factoryInfos) {
            if(item.getRemarkPic() != null && !"".equals(item.getRemarkPic())){
                String base64Image = s3UploadFileService.getImageBase64FromUrl(item.getRemarkPic());
                item.setImageBase64(CommonUtils.base64Prefix() + base64Image);
            }
        }
        return factoryInfos;
    }

    @Override
    public PcbSonFactoryInfo getById(Integer id) {
        return pcbSonFactoryInfoDao.getById(id);
    }

    @Override
    public List<PcbSonFactoryInfo> getByIds(List<Integer> ids) {

        List<PcbSonFactoryInfo> factoryInfos = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            Integer id = ids.get(i);
            PcbSonFactoryInfo sonFactoryInfo = getById(id);
            factoryInfos.add(sonFactoryInfo);
        }
        return factoryInfos;
    }

    @Transactional(transactionManager = "MysqlTransactionManager2")
    @Override
    public Result insert(PcbSonFactoryInfo param) {
        try {
            String userId = UserHeaderHolder.getUserId();
            PcbSonFactoryInfo search = new PcbSonFactoryInfo();
            search.setParentId(param.getParentId());
            search.setSonFactoryName(param.getSonFactoryName());
            List<PcbSonFactoryInfo> allByLimit = pcbSonFactoryInfoDao.getAllByLimit(search);
            if (allByLimit.stream().count() > 0) {
                return ResultUtil.error(500, "重复添加", "", null);
            } else {
                Integer parentId = param.getParentId();
                PcbParentFactoryInfo pcbParentFactoryInfo = parentFactoryInfoDao.getById(parentId);
                param.setCompanyCode(pcbParentFactoryInfo.getCompanyCode());
                param.setCompanyName(pcbParentFactoryInfo.getCompanyName());
                param.setCompanyNameEn(pcbParentFactoryInfo.getCompanyNameEn());
                param.setCompanyWebsite(pcbParentFactoryInfo.getCompanyWebsite());
                param.setCompanySort(pcbParentFactoryInfo.getSort());
                int count = pcbSonFactoryInfoDao.insert(param);
                if (count > 0) {
                    operateService.addOperate(new Operate(2, Integer.parseInt(userId), "PCB分厂管理", 1, 1, new Date(), null, JSONObject.toJSONString(param)));
                    return ResultUtil.success(param);
                } else {
                    return ResultUtil.error(500, "操作失败", "Operate Fail", null);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Transactional(transactionManager = "MysqlTransactionManager2")
    @Override
    public Result delete(Integer id) {
        try {
            String userId = UserHeaderHolder.getUserId();
            PcbApproveConfig search = new PcbApproveConfig();
            search.setSonFactoryId(id);
            List<PcbApproveConfig> pcbApproveConfigs = pcbApproveConfigDao.getAllByLimit(search);

            PcbCapacityDataApprove search2 = new PcbCapacityDataApprove();
            search2.setFactoryId(id);
            List<PcbCapacityDataApprove> pcbCapacityDataApproveList = pcbCapacityDataApproveDao.getByLimit2(search2);
            if (pcbCapacityDataApproveList.size() > 0) {
                return ResultUtil.error(500, "请先删除该厂家的审核任务", "", null);
            }
            if (pcbApproveConfigs.size() > 0) {
                return ResultUtil.error(500, "请先删除该厂家的处理人配置页面相关内容", "", null);
            }

            String oldData = JSONObject.toJSONString(pcbSonFactoryInfoDao.getById(id));
            int count = pcbSonFactoryInfoDao.delete(id);
            if (count > 0) {
                operateService.addOperate(new Operate(2, Integer.parseInt(userId), "PCB分厂管理", 2, 1, new Date(), oldData, null));
                return ResultUtil.success(null);
            } else {
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Transactional(transactionManager = "MysqlTransactionManager2")
    @Override
    public Result update(PcbSonFactoryInfo param) {
        try {
            String userId = UserHeaderHolder.getUserId();
            String oldData = JSONObject.toJSONString(pcbSonFactoryInfoDao.getById(param.getPcbSonFactoryId()));
            int count = pcbSonFactoryInfoDao.update(param);
            if (count > 0) {
                String newData = JSONObject.toJSONString(pcbSonFactoryInfoDao.getById(param.getPcbSonFactoryId()));
                operateService.addOperate(new Operate(2, Integer.parseInt(userId), "PCB分厂管理", 3, 1, new Date(), oldData, newData));
                return ResultUtil.success(null);
            } else {
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public List<Integer> getAllId() {
        return pcbSonFactoryInfoDao.getAllId();
    }


}
